<?php
/**
 * Created by PhpStorm.
 * User: angl
 * Date: 15/12/24
 * Time: 上午11:04
 */
namespace AuctionCore\Account\Back;

class Department
{

    private $id = 0;

    public function __construct($depart_id)
    {
        $this->id = $depart_id;
    }

    /**
     * 添加部门
     * @param $data
     * @return integer $last_insert_id
     */
    public static function create($data)
    {
        $data['status'] = 1;
        $data['createtime'] = date("Y-m-d H:i:s");
        return M('depart')->add($data);
    }

    /**
     * 设置部门负责人
     * @param $depart_id
     * @param $emp_id
     * @return bool
     */
    public function setLeader($depart_id, $emp_id)
    {
        if(empty($depart_id) || empty($leader)){
            return false;
        }
        $map = array('emp_id' => $emp_id);
        $data = array(
            "is_head"=>1,
            'depart_id' => $depart_id
        );
        $result = M("employee")->where($map)->save($data);
        if($result===false){
            return false;
        }
        return true;
    }

    /**
     * 获取部门详细信息
     * @return bool|mixed
     */
    public function detail()
    {
        if($this->id<1){
            return false;
        }
        return M('depart')->where("id={$this->id}")->find();
    }

    public static function getDetailByName($name)
    {
        $map = array(
            'name' => $name
        );
        return M('depart')->where($map)->find();
    }

    /**
     * 获取所有部门列表
     * @param string $cols
     * @return mixed
     */
    public static function getList($cols="*")
    {
        return  M('depart')->field($cols)->select();
    }

    /**
     * 获取下属列表
     * @param string $cols
     * @return array
     */
    public function getChildent($cols="*")
    {
        $map = array();
        $map['pid'] = $this->id;
        return M('depart')->field($cols)->where($map)->select();
    }

    /**
     * 更新部门信息
     * @param $data
     * @return bool
     */
    public function updateDepart($data)
    {
        if($this->id<1){
            return false;
        }
        return M('depart')->where("id={$this->id}")->save($data);
    }

    /**
     * 删除部门
     * @return bool
     */
    public function delete()
    {
        if($this->id<1){
            return false;
        }
        $map['id'] = $this->id;
        return M('depart')->where($map)->delete();

    }

    /**
     * 通过id获取部门信息
     * @param $id
     * @return bool|mixed
     */
    public static function getDepartById($id)
    {
        if($id<1) return false;
        return M('depart')->where("id={$id}")->find();
    }

    /**
     * 获取部门负责人
     * @return bool|mixed
     */
    public function getDepartHead()
    {
        if($this->id<1){
            return false;
        }
        $map = array(
            'depart_id' => $this->id,
            'is_head' => 1
        );
        return M('employee')->where($map)->find();
    }

    /**
     *获取部门成员
     */
    public function getDepartMember(){
        if($this->id<1){
            return false;
        }
        $list = M('employee')->field("emp_id,depart_id,mobile,real_name,is_head")->where("depart_id={$this->id}")->select();
        return $list;
    }
}